'use client' import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { toast } from 'sonner' import { deleteContractItem } from '../actions/contract-actions' import { getContractItems } from '../actions/data-actions' import { ContractSelector } from './contract-selector' import { Trash2 } from 'lucide-react' interface Contract { id: number contractNo: string contractName: string status: string projectId: number vendorId: number projectCode: string | null projectName: string | null vendorName: string | null vendorCode: string | null } interface ContractItem { id: number contractId: number itemId: number description: string | null quantity: number unitPrice: number | null ProjectNo: string | null itemCode: string | null itemName: string | null packageCode: string | null unitOfMeasure: string | null } interface ContractItemsEditFormProps { preselectedContractId?: number } export function ContractItemsEditForm({ preselectedContractId }: ContractItemsEditFormProps) { const [loading, setLoading] = useState(false) const [selectedContract, setSelectedContract] = useState() const [contractItems, setContractItems] = useState([]) // 계약 선택 시 아이템들 로드 useEffect(() => { if (selectedContract) { loadContractItems(selectedContract.id) } }, [selectedContract]) const loadContractItems = async (contractId: number) => { setLoading(true) try { const result = await getContractItems(contractId) if (result.success) { setContractItems(result.data) } else { toast.error(result.error) } } catch (error) { toast.error('계약 아이템을 불러오는 중 오류가 발생했습니다.') } finally { setLoading(false) } } const handleDelete = async (itemId: number) => { if (!confirm('정말로 이 계약 아이템을 삭제하시겠습니까?')) { return } setLoading(true) try { const result = await deleteContractItem(itemId) if (result.success) { toast.success(result.message) // 아이템 목록 새로고침 if (selectedContract) { await loadContractItems(selectedContract.id) } } else { toast.error(result.error) } } catch (error) { toast.error('계약 아이템 삭제 중 오류가 발생했습니다.') } finally { setLoading(false) } } return ( 계약 아이템 삭제 기존 계약의 아이템들을 삭제할 수 있습니다.
{/* 계약 선택 */}
{selectedContract && (
선택된 계약
[{selectedContract.contractNo}] {selectedContract.contractName}
)} {/* 계약 아이템 목록 */} {contractItems.length > 0 && (
{contractItems.map(item => (
{item.itemName || `아이템 ${item.itemId}`} {item.itemCode && ( {item.itemCode} )} {item.unitOfMeasure && ( {item.unitOfMeasure} )}
{item.ProjectNo && (
프로젝트: {item.ProjectNo} | 패키지: {item.packageCode}
)} {/* 보기 모드만 유지 */}
수량: {item.quantity} | 단가: {item.unitPrice || 0}
{item.description && (
설명: {item.description}
)}
{/* 삭제 버튼만 유지 */}
))}
)} {selectedContract && contractItems.length === 0 && !loading && (
선택된 계약에 아이템이 없습니다.
)} {loading && (
로딩 중...
)}
) }